﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Database_modyfication : Form
    {
        public Database_modyfication()
        {
            InitializeComponent();
            this.FillServerComboBox();
        }

        SQLConnection sqlConnection = new SQLConnection();
        SQLModifications databaseModyfications = new SQLModifications();
        WebConfigParser webConfigParser = new WebConfigParser();
        List<string> serverConnectionStringList = new List<string>();
        List<string> databasesList = new List<string>();
        List<Guid> guidList = new List<Guid>();// tu jest list GUIDÓW
        List<int> clientList = new List<int>(); //tu jest lista klientów
        List<int> fatureGruopsIDList = new List<int>();
        List<string> listOfTablesToModify = new List<string>();

        private void FillServerComboBox()
        {
            this.serverConnectionStringList = sqlConnection.GetServerComboBoxFromFile("serverConnection.txt");

            foreach (string serverConnectionStringListItem in serverConnectionStringList)
            {
                serverCSComboBox.Items.Add(serverConnectionStringListItem);
            }
        }

        /// <summary>
        /// Wybór connection string serwera z dostępnej listy.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void serverCSComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            sqlConnection.serverConnectionString = serverCSComboBox.SelectedItem.ToString();
            databaseModyfications.serverConnectionString = serverCSComboBox.SelectedItem.ToString(); // to jest bardzo wazne
            this.databasesList = sqlConnection.GetDatabasesComboBox();
            this.ClearComboBoxes(1);
            this.ClearComboBoxes(0);
            //databasesComboBox.Text = string.Empty;
            //listOfTablesToModifyComboBox.Items.Clear();
            //listOfTablesToModifyComboBox.Text = string.Empty;
            //guidsComboBox.Items.Clear();
            //guidsComboBox.Text = string.Empty;
            //clientsComboBox.Items.Clear();
            //clientsComboBox.Text = string.Empty;
            //osDatabasesNameComboBox.Text = string.Empty;
            //osDatabasesNameComboBox.Items.Clear();

            //databasesComboBox.Items.Clear();

            foreach (string databasesListItem in databasesList)
            {
                if (databasesListItem.Contains("Textures"))
                {
                databasesComboBox.Items.Add(databasesListItem);
                }
            }

            foreach (string databasesListItem in databasesList)
            {
                if (databasesListItem.Contains("OnlineServices"))
                {
                    osDatabasesNameComboBox.Items.Add(databasesListItem);
                }
            }
        }



        /// <summary>
        /// Wybór bazy danych z dostępnej listy.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void databasesComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            var model = new Models.TexturesEntities(); //TODO: tu wrzuć connection stringvR 
            var asd = model.DeveloperAuthentication.Where(d => d.DeveloperAuthenticationId == 1).ToList(); //zmienna symbolizuje wiersz
            // developerauth... to propertis. znajdz wszystkie wiersze ktore spelniaja warunek
            // modyfikCJ...
            model.SaveChanges();


            var newdata = new Models.DeveloperAuthentication(); // nowy wiersz
            newdata.DeveloperAuthenticationUserIdentity = Guid.Empty;
            model.DeveloperAuthentication.AddObject(newdata);
            model.SaveChanges();

            if (databasesComboBox.SelectedIndex != -1)
            {
                guidsComboBox.Items.Clear();
                clientsComboBox.Items.Clear();
                this.ClearComboBoxes(1);
                //this.ClearComboBoxes(0);
                string databaseName = databasesComboBox.SelectedItem.ToString(); //
                databaseModyfications.databaseName = databaseName; //przekazuje wybrana nazwe bazy do klasy databasemodyfications
                this.guidList = databaseModyfications.GetGUIDSList();
                this.clientList = databaseModyfications.GetClientsList();
                this.fatureGruopsIDList = databaseModyfications.GetFeatureGroupUserIDList();
                this.listOfTablesToModify = databaseModyfications.GetListOfTablesToModify();
               // webConfigParser.GetDatabaseName(databaseName);

                //TODO: tu będzie można wybrać któremu userowi nadac jakiego defaultowego klienta. Dla wygody nie powinny byc numerki
                foreach (Guid guidListItem in guidList) //wypełniam comboboxa guidami
                {
                    guidsComboBox.Items.Add(guidListItem).ToString();
                }

                foreach (int clientListItem in clientList)// wypełniam comboboxa klientami
                {
                    clientsComboBox.Items.Add(clientListItem).ToString();
                }

                foreach (string tabelToModifyListItem in listOfTablesToModify)// wypełniam comboboxa tabelami
                {
                    listOfTablesToModifyComboBox.Items.Add(tabelToModifyListItem).ToString();
                }

                if (guidsComboBox.Items.Count != 0)
                {
                    assignDefaultClientGroupBox.Show();
                }

                if (listOfTablesToModifyComboBox.Items.Count != 0)
                {
                    addGUIDsToTablesGroupBox.Show();
                }

                if (osDatabasesNameComboBox.Items.Count != 0)
                {
                    usersViewModyfication.Show();
                }


                //databaseModyfications.FillGUIDS();
            }
        }

        private void ClearComboBoxes(int grade)
        {
            if (grade == 0)
            {
                databasesComboBox.Items.Clear();
                databasesComboBox.Text = string.Empty;
                //serverCSComboBox.Items.Clear();
                //serverCSComboBox.Text = string.Empty;
            }
            else if (grade == 1)
            {
                guidsComboBox.Items.Clear();
                guidsComboBox.Text = string.Empty;
                clientsComboBox.Items.Clear();
                clientsComboBox.Text = string.Empty;
                listOfTablesToModifyComboBox.Items.Clear();
                listOfTablesToModifyComboBox.Text = string.Empty;
                osDatabasesNameComboBox.Text = string.Empty;
                osDatabasesNameComboBox.Items.Clear();
            }           
        }

        /// <summary>
        /// Zapisz zmiany do pliku web.config
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveWebConfigButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void editServerCSButton_Click(object sender, EventArgs e)
        {
            //webConfigPanel.Hide();
            serverConnectionStringEditPanel.Location = new Point(9, 12);
            serverConnectionStringEditPanel.Visible = true;
            webConfigPanel.Visible = false;
            serverConnectionStringsRichTextBox.LoadFile("serverConnection.txt", RichTextBoxStreamType.PlainText);
        }

        private void saveServerConnectionString_Click(object sender, EventArgs e)
        {
            serverConnectionStringEditPanel.Visible = false;
            webConfigPanel.Visible = true;
            serverConnectionStringsRichTextBox.SaveFile("serverConnection.txt", RichTextBoxStreamType.PlainText);
            this.ClearComboBoxes(0);
            FillServerComboBox();
        }

        private void cancelServerConnectionString_Click(object sender, EventArgs e)
        {
            serverConnectionStringEditPanel.Visible = false;
            webConfigPanel.Visible = true;
        }

        private void setDafaultButton_Click(object sender, EventArgs e)
        {
            if (guidsComboBox.Text != null && clientsComboBox.Text != null)
            {
             //   databaseModyfications.SetDefaultClientUser(Guid.Parse(guidsComboBox.Text), int.Parse(clientsComboBox.Text));
            }
            //TODO: przed wyknoaniem trzeba sprawdzić czy user już ma defaultowego klienta
            //TODO: user juz ma defaultowego klienta - czy wtedy zmienic
            //TODO: nie mozna zapisać gdy user nie podał usera i klienta
        }

        private void ClientUserButton_Click(object sender, EventArgs e)
        {
            if (listOfTablesToModifyComboBox.Text == "ClientUser")
            {
                databaseModyfications.InsertIntoClientUser();
            }
            else if (listOfTablesToModifyComboBox.Text == "FeatureGroupUser")
            {
                databaseModyfications.InsertIntoFeatureGroupUser();
            }
            else if (listOfTablesToModifyComboBox.Text == "DeveloperAuthentication")
            {
            }
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void osDatabasesNameComboBox_SelectedValueChanged(object sender, EventArgs e)
        {
            string onlineServicesDatabaseName = osDatabasesNameComboBox.SelectedItem.ToString(); //
            databaseModyfications.onlineServicesDatabaseName = onlineServicesDatabaseName;
        }

        private void replaceButton_Click(object sender, EventArgs e)
        {

        }

        
    }
}
